<?php

namespace App\Console\Commands\Init;

use App\Common\Libs\Json;
use App\Console\BaseCommand;
use App\Models\SysJob;
use Illuminate\Support\Facades\Log;

/**
 * 【V2.0.5】职业迁移脚本
 * Class ArticleCounterCommand
 * @package App\Console\Scripts\Counter
 */
class JobInitCommand extends BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @example php artisan init:job all
     * [!] all 代表执行所有公开方法
     * @var string
     */
    protected $signature = 'init:job {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'init job';

    /**
     * 静态化
     * @return int
     */
    public function static()
    {
        $firstJobs = SysJob::getInstance()->where('parent_id', 0)->get();
        $data = [];
        foreach ($firstJobs as $firstJob) {
            $firstData = [
                'id' => $firstJob->id,
                'parent_id' => $firstJob->parent_id,
                'name' => $firstJob->name,
                'level' => $firstJob->level,
            ];

            $secondJobs = SysJob::getInstance()->where('parent_id', $firstJob->id)->get();
            foreach ($secondJobs as $secondJob) {
                $secondJob->level = 2;
                $secondJob->save();
                $secondJobArr = [
                    'id' => $secondJob->id,
                    'parent_id' => $secondJob->parent_id,
                    'name' => $secondJob->name,
                    'level' => $secondJob->level,
                ];
                $thirdJobs = SysJob::getInstance()->where('parent_id', $secondJob->id)->get();
                $thirdJobArr = [];
                foreach ($thirdJobs as $thirdJob) {
                    $thirdJob->level = 3;
                    $thirdJob->save();
                    $thirdJobArr[] = [
                        'id' => $thirdJob->id,
                        'parent_id' => $thirdJob->parent_id,
                        'name' => $thirdJob->name,
                        'level' => $thirdJob->level,
                    ];
                }
                $secondJobArr['child'] = $thirdJobArr;
                $firstData['child'][] = $secondJobArr;
            }
            $data[] = $firstData;
        }

        Log::channel('command')->info(Json::encode($data));

        // 执行成功返回 1
        return 1;
    }

}
